package com.qq.client.model;

import com.qq.common.Message;
import com.qq.common.MessageType;
import com.qq.common.User;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * @author weiBin
 * @date 2019/1/9
 */
public class QqClientConServer {
    /**
     * 发送短信到服务端
     *
     * @param user
     * @return
     */
    public boolean sendLoginInfoToServer(User user) {
        boolean flag = false;
        Socket socket =null;
        try {
            socket = new Socket("127.0.0.1", 8888);
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(user);
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            Message ms = (Message) ois.readObject();
            if (MessageType.message_succeed.equals(ms.getMesType())) {
                System.out.println("登陆成功!");
                ClientConServerThread ccst = new ClientConServerThread(socket);
                ccst.start();
                ManageClientConServerThread.setClientConServerThread(user.getNo(),ccst);
                flag = true;
            } else {
                //关闭Scoket
                socket.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
}

